<%--
  Created by IntelliJ IDEA.
  User: 27716
  Date: 2024/10/31
  Time: 20:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加</title>
    <script src="../../static/js/jquery-1.8.2.min.js"></script>
    <link rel="stylesheet" href="../../static/css/css.css">
    <script>
        $(function (){
            $("#purchaseCode").html(getPurchaseDate());
            getSupplier();
        })
        function getPurchaseDate(){
            //单号 规则：JH + 日期 + 随机数
            let date = new Date();
            let year = date.getFullYear();
            let month = date.getMonth()+1;
            let day = date.getDate();
            //补0操作
            month = month<10?"0"+month:month;
            day = day<10?"0"+day:day;
            //随机数
            let number = Math.floor(Math.random()*9999-1000+1)+1000;
            //组合
            return "JH"+year+month+day+number;
        }
        function getSupplier(){
            $.ajax({
                url: "/supplier/getSupplier",
                dataType: "json",
                success(res){
                    console.log(res);
                    for (let o of res) {
                        $("#supplierId").append(`
                            <option value="\${o.supplierId}" data-aa=\${o.address}>\${o.supplierName}</option>
                        `)
                    }
                }
            })
        }
    </script>
</head>
<body>
<table>
    <tr>
        <td>单号</td>
        <td>
            <span id="purchaseCode"></span>
        </td>
        <td>供应商</td>
        <td>
            <select id="supplierId" onchange="getAddRes()">
                <option value="">请选择</option>
            </select>
        </td>
        <td>应付金额</td>
        <td>
            <span id="purchaseSumPrice"></span>
        </td>
    </tr>
    <tr>
        <td>实付金额</td>
        <td>
            <span id="payMoney"></span>
        </td>
        <td>收货日期</td>
        <td>
            <input type="datetime-local" step="1" id="purchaseTime">
        </td>
        <td>备注</td>
        <td>
            <textarea id="address"></textarea>
        </td>
    </tr>
    <tr>
        <td>折扣</td>
        <td>
            <select id="discount" onchange="getChange()">
                <option value="1">无折扣</option>
                <option value="0.9">九折</option>
                <option value="0.95">九五折</option>
                <option value="0.85">八五折</option>
                <option value="0.8">八折</option>
            </select>
        </td>
        <td>下单人</td>
        <input type="hidden" id="uid" value="${user.uid}">
        <td>
            ${user.username}
        </td>
        <td>购买总数
        <span id="purchaseNum"></span>
        </td>
        <td colspan="100">
            <input type="button" value="选择商品" onclick="findSupplier()">
        </td>
    </tr>
</table>
<table id="table">
    <tr>
        <td>商品编号id</td>
        <td>商品编码</td>
        <td>商品名称</td>
        <td>库存数量</td>
        <td>采购价格</td>
    </tr>
</table>
</body>
<script>
    function getAddRes(){
        let supplierId = $("#supplierId option:selected").data("aa");
        $("#address").html(supplierId);
    }
    //显示供应商
    function findSupplier(){
        let supplierId = $("#supplierId").val();
        if(!supplierId){
            alert("请先选择供应商");
            return;
        }
        let obj = {supplierId};
        $.ajax({
            url:"/goods/findGoodsBySupplierId",
            type:"post",
            data:{reqInfos:JSON.stringify(obj)},
            dataType: "json",
            success(res) {
                console.log(res);
                $("#table").empty();
                $("#table").append(`
                    <tr>
                        <td>
                            <input type="checkbox" id="check" onchange="getPayMoney()">
                        </td>
                        <td>商品编号id</td>
                        <td>商品编码</td>
                        <td>商品名称</td>
                        <td>库存数量</td>
                        <td>采购价格</td>
                        <td>采购数量 （getPrice）</td>
                        <td>采购总价格 (sumPrice)</td>
                    </tr>
                `)
                for (let o of res) {
                    $("#table").append(`
                    <tr>
                        <td>
                            <input type="checkbox" class="ck" value="\${o.goodsId}" onchange="getPayMoney()">
                        </td>
                        <td>\${o.goodsId}</td>
                        <td>\${o.goodsCode}</td>
                        <td>\${o.goodsName}</td>
                        <td>\${o.inventoryQuantity}</td>
                        <td>\${o.purchasingPrice}</td>
                        <td>
                            <input type="text" id="num\${o.goodsId}" onchange="getPrice(\${o.purchasingPrice},this,\${o.goodsId})">
                        </td>
                        <td>
                            <span id="sumPrice\${o.goodsId}">0</span>
                        </td>
                    </tr>
                `)
                }
                //循环结束之后追加按钮
                $("#table").append(`
                    <tr>
                        <td colspan="100">
                            <input type="button" value="确定入库" onclick="getAddPurchase()">
                            <input type="button" value="返回" onclick="history.back()">
                        </td>
                    </tr>
                `)
            },
            error(){
                alert("宝宝，服务器错误了")
            }
        })
    }
</script>
<script>
    //全选 全不选
    $(document).on('click',"#check",function (){
        $(".ck").prop("checked",this.checked);
    })
    //计算购买总价格
    function getPrice(price,num,goodsId) {
        //获取this的值
        let n = $(num).val();
        if(n<=0){
            alert("一个不买滚出去");
            $(num).val(null);
            return;
        }
        $("#sumPrice"+goodsId).html((price * n).toFixed(2));
    }
    //计算应付金额 计算总数量
    function getPayMoney(){
        let sum = 0.0;
        let count = 0;
        $(".ck:checked").each(function (){
            let goodsId = $(this).val();
            let sumPrice = $("#sumPrice"+goodsId).html();
            let num = $("#num"+goodsId).val();
            //获取数量
            count += +num;
            //累积求和
            sum += +sumPrice;

        })
        //应付金额
        $("#purchaseSumPrice").html(sum);
        //实付金额
        $("#payMoney").html(sum);
        //购买总数
        $("#purchaseNum").html(count);
    }
    function getChange(){
        let discount = $("#discount").val();
        let purchaseSumPrice = $("#purchaseSumPrice").html();
        $("#payMoney").html((purchaseSumPrice*discount).toFixed(2))
    }

    //确定入库
    function getAddPurchase(){
        let purchaseCode = $("#purchaseCode").html();
        let supplierId = $("#supplierId").val();
        let purchaseSumPrice = $("#purchaseSumPrice").html();
        let payMoney = $("#payMoney").html();
        let purchaseTime = $("#purchaseTime").val();
        let address = $("#address").val();
        let discount = $("#discount").val();
        let uid = $("#uid").val();
        let purchaseNum = $("#purchaseNum").html();

        let arr = [];
        $(".ck:checked").each(function (){
            let goodsId = $(this).val();
            let num = $("#num"+goodsId).val();
            let money = $("#sumPrice"+goodsId).val();
            let obj = {goodsId,num,money};
            arr.push(obj);
            if(!purchaseCode||!supplierId||!purchaseSumPrice||!payMoney||!purchaseTime||!address||!discount||!uid){
                alert("数据不能为空");
                return
            }
            if(!arr.length){
                alert("请先选择要 购买的商品");
                return;
            }
        })
        let obj = {
            purchaseCode,
            supplierId,
            purchaseSumPrice,
            payMoney,
            purchaseTime,
            address,
            discount,
            uid,
            purchaseNum,
            arr
        }
        $.ajax({
            url:"/purchases/addPurchases",
            type: "post",
            data:{reqInfos:JSON.stringify(obj)},
            dataType:"json",
            success(res){
                console.log(res);
                if(res === 200){
                    alert("添加成功");
                    location.href = "list.jsp";
                }
            },
            error(){
                alert("服务器故障")
            }
        })
    }
</script>
</html>
